package com.youlai.boot.modules.category.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.modules.category.mapper.CategoryMapper;
import com.youlai.boot.modules.category.model.Category;
import com.youlai.boot.modules.category.model.CategoryPageQuery;
import com.youlai.boot.modules.category.model.CategoryVO;
import com.youlai.boot.modules.category.model.TreeItemVO;
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;
import com.youlai.boot.modules.category.service.CategoryService;
import com.youlai.boot.modules.category.model.CategoryForm;
import com.youlai.boot.modules.category.model.CategoryConverter;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    private final CategoryConverter categoryConverter;

    @Override
    public Page<CategoryVO> getCategoryPage(CategoryPageQuery queryParams) {
        Page<CategoryVO> page = new Page<>(queryParams.getPageNum(), queryParams.getPageSize());
        return baseMapper.getCategoryPage(page, queryParams);
    }

    @Override
    public CategoryVO getCategoryById(Long categoryId) {
        Category category = this.getById(categoryId);
        return categoryConverter.toVO(category);
    }

    @Override
    public CategoryForm getCategoryForm(Long categoryId) {
        Category entity = this.getById(categoryId);
        return categoryConverter.toForm(entity);
    }

    @Override
    public Long saveCategory(CategoryForm form) {
        Category entity = categoryConverter.toEntity(form);
        this.save(entity);
        return entity.getId();
    }

    @Override
    public void updateCategory(CategoryForm form) {
        Category entity = categoryConverter.toEntity(form);
        this.updateById(entity);
    }

    @Override
    public boolean deleteCategory(Long categoryId) {
        return this.removeById(categoryId);
    }

    @Override
    public List<TreeItemVO> getCategoryTree() {
        // 1. 获取所有type=1和type=2的分类
        List<Category> allCategories = this.lambdaQuery()
                .in(Category::getType, Arrays.asList(1, 2))
                .eq(Category::getShowFlag, 1)
                .orderByAsc(Category::getSort)
                .list();

        // 2. 构建树形结构
        List<TreeItemVO> tree = allCategories.stream()
                .filter(category -> category.getType() == 1) // 筛选第一层
                .map(level1 -> {
                    // 使用转换器转换一级分类
                    TreeItemVO treeItem = categoryConverter.toTreeItem(level1);

                    // 找到并转换二级分类
                    List<TreeItemVO> children = allCategories.stream()
                            .filter(category -> category.getType() == 2
                                    && category.getParentId().equals(level1.getId())
                                    && category.getShowFlag() == 1)
                            .map(categoryConverter::toTreeItem) // 使用方法引用简化转换
                            .collect(Collectors.toList());

                    treeItem.setChildren(children);
                    return treeItem;
                })
                .collect(Collectors.toList());

        return tree;
    }
}
